#!/bin/bash
[ -z $(getenforce|grep 'disabled') ] && setenforce 0 && sed -i 's/^SELINUX=.*$/SELINUX=disabled/g' /etc/selinux/config
echo '下载李白'
yum install libaio -y 
echo '下载好了'
mkdir -p /usr/local/mysql
echo '开始解压'
tar -xf mysql-8.0.40-linux-glibc2.17-x86_64.tar.xz -C /usr/local/mysql
echo '解压完成'
cd /usr/local/mysql && mv *mysql* mysql
useradd -r -M -s /sbin/nologin mysql
echo '开始清空'
rm -f /etc/my.cnf && yum remove my.cnf && yum remove mariadb
echo '清空完成'
echo '开始初始化'
mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql &> /tmp/mysqld.log
echo '初始化完成'
mysql/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/mysql/data &> /dev/null
cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql/mysql
datadir=/usr/local/mysql/mysql/data
socket=/tmp/mysql.sock
character_set_server=utf8
collation-server=utf8_unicode_ci
EOF
cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
After=network.target

[Service]
User=mysql
Group=mysql
Type=forking

# MySQL 执行命令及路径
ExecStart=/usr/local/mysql/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysql/data/mysqld.pid
ExecStop=/usr/local/mysql/mysql/bin/mysqladmin --defaults-file=/etc/my.cnf shutdown

# Ensure MySQL has sufficient time to start up
TimeoutSec=600

# PID 文件路径
PIDFile=/usr/local/mysql/mysql/data/mysqld.pid

# Enable these options to auto-restart the service if it crashes
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
echo '正在重启服务'
systemctl daemon-reload
systemctl restart mysqld
systemctl enable mysqld
echo '成功重启'
echo 'export PATH=$PATH:/usr/local/mysql/mysql/bin' >> /etc/profile
source /etc/profile
echo '正在配置新密码'
pass=$(grep 'password' /tmp/mysqld.log|awk '{print $NF}')
new_pass=123456
mysqladmin -uroot password $new_pass  -p$pass
ln -s /lib64/libncurses.so.6 /lib64/libncurses.so.5
ln -s /lib64/libtinfo.so.6 /lib64/libtinfo.so.5
echo '配置完成'
